﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.NannyModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using FluentValidation.Attributes;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using System.ComponentModel;

    [Serializable]
    [Validator(typeof(NannyValidator))]
    public class Nanny : Persistent
    {
        public virtual UserAccount UserAccount { get; set; }
        public virtual Corporation Corporation { get; set; }
        [DisplayName("姓名")]
        public virtual string Name { get; set; }
        [DisplayName("年龄")]
        public virtual int Age { get; set; }
        [DisplayName("性别")]
        public virtual NannyIdentifier.Sex Sex { get; set; }
        [DisplayName("民族")]
        public virtual int Nation { get; set; }
        [DisplayName("出生日期")]
        public virtual DateTime Birthday { get; set; }
        [DisplayName("手机")]
        public virtual string Telephone { get; set; }
        [DisplayName("电话")]
        public virtual string Mobile { get; set; }
        [DisplayName("QQ")]
        public virtual string QQ { get; set; }
        [DisplayName("MSN")]
        public virtual string MSN { get; set; }
        [DisplayName("联系公司")]
        public virtual bool IsContactCompany { get; set; }
        [DisplayName("星座")]
        public virtual int Astrology { get; set; }
        [DisplayName("计算机水平")]
        public virtual int ComputerLevel { get; set; }
        [DisplayName("生肖")]
        public virtual int ChineseZodiac { get; set; }
        [DisplayName("是否已婚")]
        public virtual bool IsMarried { get; set; }
        [DisplayName("语言")]
        public virtual long Language { get; set;}
        [DisplayName("地区")]
        public virtual Area Area { get; set; }
        [DisplayName("地址")]
        public virtual string Address { get; set; }
        [DisplayName("学历")]
        public virtual int Degree { get; set; }
        [DisplayName("驾照")]
        public virtual bool CanDrive { get; set; }
        [DisplayName("已有小孩")]
        public virtual bool HasChildren { get; set; }
        [DisplayName("健康状况")]
        public virtual int Healthy { get; set; }
        [DisplayName("宗教")]
        public virtual int Faith { get; set; }
        [DisplayName("擅长技能")]
        public virtual long MainSkill { get; set; }
        public virtual long FirstSkill { get; set; }
        public virtual long SecondSkill { get; set; }
        public virtual long ThirdSkill { get; set; }
        [DisplayName("培训经历")]
        public virtual bool IsTrained { get; set; }
        [DisplayName("工作经验")]
        public virtual int Experience { get; set; }
        [DisplayName("薪资要求")]
        public virtual int Salary { get; set; }
        [DisplayName("个人介绍")]
        public virtual string Introduce { get; set; }
        [DisplayName("资格证书")]
        public virtual long Certificate { get; set; }
        [DisplayName("个人宣言")]
        public virtual string Declaration { get; set; }
        [DisplayName("家政类型")]
        public virtual NannyIdentifier.NannyType Type { get; set; }
        public virtual bool IsWorking { get; set; }
        [DisplayName("需提供住宿")]
        public virtual bool IsNeedAccommodate { get; set; }
        [DisplayName("需工作餐")]
        public virtual bool IsNeedMeal { get; set; }
        public virtual int SatisfactoryTotal { get; set; }
        public virtual int CommentTotal { get; set; }
        public virtual int ViewTotal { get; set; }
        public virtual int SearchTotal { get; set; }
        public virtual bool IsDeleted { get; set; }
        public virtual NannyStatus NannyStatus { get; set; }
        public virtual IList<NannyComment> Comments { get; set; }
        public virtual IList<NannyDiary> Diarys { get; set; }
        public virtual IList<Consign> Consigns { get; set; }
        public virtual IList<Attachment> Attachments { get; set; }

        public Nanny()
        {

        }

        public virtual BoolGenericResult<Nanny> ModifyInof(
                                                    string name,
                                                    int age,
                                                    NannyIdentifier.Sex sex,
                                                    int nation,
                                                    DateTime birthday,
                                                    string telephone,
                                                    string mobile,
                                                    string qq,
                                                    string msn,
                                                    bool isContactCompany,
                                                    int astrology,
                                                    int computerLevel,
                                                    int chineseZodiac,
                                                    bool isMarried,
                                                    long language,
                                                    Area area,
                                                    string address,
                                                    int degree,
                                                    bool canDrive,
                                                    bool hasChildren,
                                                    int healthy,
                                                    int faith,
                                                    long mainSkill,
                                                    long firstSkill,
                                                    long secondSkill,
                                                    long thirdSkill,
                                                    bool isTrained,
                                                    int experience,
                                                    int salary,
                                                    string introduce,
                                                    long certificate,
                                                    string declaration,
                                                    NannyIdentifier.NannyType type,
                                                    bool isWorking,
                                                    bool isNeedAccommodate,
                                                    bool isNeedMeal)
        {
            if (area == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<Nanny> result = new BoolGenericResult<Nanny>();
            this.Name = name;
            this.Age = age;
            this.Sex = sex;
            this.Nation = nation;
            this.Birthday = birthday;
            this.Telephone = telephone;
            this.Mobile = mobile;
            this.QQ = qq;
            this.MSN = msn;
            this.IsContactCompany = isContactCompany;
            this.Astrology = astrology;
            this.ComputerLevel = computerLevel;
            this.ChineseZodiac = chineseZodiac;
            this.IsMarried = isMarried;
            this.Language = language;
            this.Area = area;
            this.Address = address;
            this.Degree = degree;
            this.CanDrive = canDrive;
            this.HasChildren = hasChildren;
            this.Healthy = healthy;
            this.Faith = faith;
            this.MainSkill = mainSkill;
            this.FirstSkill = firstSkill;
            this.SecondSkill = secondSkill;
            this.ThirdSkill = thirdSkill;
            this.IsTrained = isTrained;
            this.Experience = experience;
            this.Salary = salary;
            this.Introduce = introduce;
            this.Certificate = certificate;
            this.Declaration = declaration;
            this.Type = type;
            this.IsWorking = isWorking;
            this.IsNeedAccommodate = isNeedAccommodate;
            this.IsNeedMeal = isNeedMeal;

            this.NannyStatus.BasicInfoStatus = NannyIdentifier.CheckedStatus.Modified;

            result.Object = this;

            return result;
        }

        public virtual BoolGenericResult<Attachment> ModifyAvatar(
                                                        string fileUrl, 
                                                        string fileName)
        {
            BoolGenericResult<Attachment> result = new BoolGenericResult<Attachment>();
            Attachment avatar = this.Attachments.FirstOrDefault(a => a.OwnerID == this.ID && a.AttachmentGroup == AttachmentIdentifier.AttachmentGroup.NannyAvatar);
            avatar.FileUrl = fileUrl;
            avatar.FileName = fileName;
            result.Object = avatar;

            return result;
        }

        public virtual BoolGenericResult<Attachment> UploadPhoto(
                                                        string fileUrl,
                                                        string fileName)
        {
            BoolGenericResult<Attachment> result = new BoolGenericResult<Attachment>();
            Attachment photo = AttachmentFactory.CreateAttachment(AttachmentIdentifier.AttachmentGroup.NannyPhoto, this.ID, fileName, fileUrl, true, AttachmentIdentifier.CheckedStatus.UnChecked, 
                                                    false, 0, DateTime.Now, this.UserAccount.Name);
            result.Object = photo;

            return result;
        }

        public virtual BoolGenericResult<Attachment> ModifyPhoto(
                                            int photoID,
                                            string fileUrl,
                                            string fileName)
        {
            BoolGenericResult<Attachment> result = new BoolGenericResult<Attachment>();
            Attachment photo = this.Attachments.FirstOrDefault(a => a.ID == photoID && a.AttachmentGroup == AttachmentIdentifier.AttachmentGroup.NannyPhoto);
            photo.FileUrl = fileUrl;
            photo.FileName = fileName;

            result.Object = photo;

            return result;
        }

        public virtual BoolGenericResult<Attachment> DeletePhoto(Attachment photo)
        {
            if (photo == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<Attachment> result = new BoolGenericResult<Attachment>();
            photo.IsDeleted = true;
            result.Object = photo;

            return result;
        }

        public virtual BoolGenericResult<ConsignBidder> BidConsign(
                                                            Consign consign)
        {
            if (consign == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<ConsignBidder> result = new BoolGenericResult<ConsignBidder>();
            if (this.NannyStatus.BidStatus == NannyIdentifier.BidStatus.CannotBid)
            {
                result.IsSuccess = false;
                result.Messages.Add("您当前为禁止竞标状态，请先联系公司更新为可竞标！");
                return result;
            }
            ConsignBidder consignBidder = ConsignBidderFactory.CreateConsignBidder(consign, this, this.Corporation, ConsignIdentifier.ConsignBidderStatus.IsNew, false, DateTime.Now,
                this.UserAccount.Name);
            result.Object = consignBidder;

            return result;
        }

        public virtual BoolGenericResult<ConsignBidder> CancelConsign(
                                                            Consign consign)
        {
            if (consign == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<ConsignBidder> result = new BoolGenericResult<ConsignBidder>();
            ConsignBidder consignBidder = consign.ConsignBidders.FirstOrDefault(cb => cb.Bidder.ID == this.ID);
            if (consign.ConsignStatus == ConsignIdentifier.ConsignStatus.IsBidding)
            {
                if (consignBidder.ConsignBidderStatus == ConsignIdentifier.ConsignBidderStatus.IsNew)
                {
                    consignBidder.ConsignBidderStatus = ConsignIdentifier.ConsignBidderStatus.IsCanceled;
                    consign.BidCount = consign.BidCount - 1;
                }
            }

            result.Object = consignBidder;

            return result;
        }

        public virtual BoolGenericResult<ConsignBidder> ResumeBidConsign(
                                                    Consign consign)
        {
            if (consign == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<ConsignBidder> result = new BoolGenericResult<ConsignBidder>();
            ConsignBidder consignBidder = consign.ConsignBidders.FirstOrDefault(cb => cb.Bidder.ID == this.ID);
            if (consign.ConsignStatus == ConsignIdentifier.ConsignStatus.IsBidding)
            {
                if (consignBidder.ConsignBidderStatus == ConsignIdentifier.ConsignBidderStatus.IsCanceled)
                {
                    consignBidder.ConsignBidderStatus = ConsignIdentifier.ConsignBidderStatus.IsNew;
                    consign.BidCount = consign.BidCount + 1;
                }
            }
            result.Object = consignBidder;

            return result;
        }

        public virtual BoolGenericResult<NannyDiary> PublishDiary(
                                                        string title,
                                                        string content)
        {
            BoolGenericResult<NannyDiary> result = new BoolGenericResult<NannyDiary>();
            NannyDiary nannyDiary = NannyDiaryFactory.CreateNannyDiary(this, title, content, Project.Settings.DefaultNannyDiaryViewTotal, NannyIdentifier.DiaryStatus.OnShow, DateTime.Now, this.UserAccount.Name);
            result.Object = nannyDiary;

            return result;
        }

        public virtual BoolGenericResult<NannyDiary> ModifyDiary(
                                                NannyDiary nannyDiary,
                                                string title,
                                                string content)
        {
            if (nannyDiary == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<NannyDiary> result = new BoolGenericResult<NannyDiary>();
            nannyDiary.Title = title;
            nannyDiary.Content = content;

            result.Object = nannyDiary;

            return result;
        }

        public virtual BoolGenericResult<NannyDiary> DeleteDiary(
                                                        NannyDiary nannyDiary)
        {
            if (nannyDiary == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<NannyDiary> result = new BoolGenericResult<NannyDiary>();
            nannyDiary.DiaryStatus = NannyIdentifier.DiaryStatus.IsDeleted;
            result.Object = nannyDiary;

            return result;
        }

        public virtual BoolGenericResult<NannyStatus> RefreshSelf()
        {
            BoolGenericResult<NannyStatus> result = new BoolGenericResult<NannyStatus>();
            if (!Project.Settings.IsFree)
            {
                if (this.NannyStatus.RefreshCount >= this.Corporation.UserAccount.UserServices[0].RefreshPerDay)
                {
                    result.IsSuccess = false;
                    result.Messages.Add("刷新的次数已达上限，不与再刷新！");
                    return result;
                }
            }
            if (this.NannyStatus.RefreshTime.Day != DateTime.Now.Day &&
                this.NannyStatus.RefreshTime.Month != DateTime.Now.Month &&
                this.NannyStatus.RefreshTime.Year != DateTime.Now.Year)
            {
                this.NannyStatus.RefreshCount = 0;
            }
            this.NannyStatus.RefreshTime = DateTime.Now;
            this.NannyStatus.RefreshCount = this.NannyStatus.RefreshCount + 1;

            result.Object = this.NannyStatus;

            return result;
        }

        public virtual BoolGenericResult<Nanny> DoWork(bool isWorking)
        {
            BoolGenericResult<Nanny> result = new BoolGenericResult<Nanny>();
            this.IsWorking = isWorking;
            if (isWorking)
            {
                this.NannyStatus.OnLineStatus = NannyIdentifier.OnLineStatus.OffLine;
            }
            else
            {
                this.NannyStatus.OnLineStatus = NannyIdentifier.OnLineStatus.OnLine;
            }
            result.Object = this;

            return result;
        }

        public virtual BoolGenericResult<UserMessage> LeaveMessage(
                                                            UserAccount ownerUserAccount,
                                                            string content)
        {
            if (ownerUserAccount == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<UserMessage> result = new BoolGenericResult<UserMessage>();
            UserMessage userMessage = UserMessageFactory.CreateUserMessage(ownerUserAccount, content, 0, 0, this.UserAccount, false, DateTime.Now, this.UserAccount.Name);

            result.Object = userMessage;

            return result;
        }

        public virtual BoolGenericResult<UserMessage> ReplyMessage(UserMessage userMessage, string content)
        {
            if (userMessage == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<UserMessage> result = new BoolGenericResult<UserMessage>();
            UserMessage repliedMessage = UserMessageFactory.CreateUserMessage(this.UserAccount, content, userMessage.ID, 0, this.UserAccount, false, DateTime.Now, this.UserAccount.Name);
            result.Object = repliedMessage;
            userMessage.Reply = userMessage.Reply + 1;

            return result;
        }

        public virtual BoolGenericResult<Note> SendNote(
                                            string title,
                                            string content,
                                            InformationIdentifier.NoteType type,
                                            IList<NoteReceiver> noteReceivers)
        {
            if (noteReceivers == null)
            {
                throw new ArgumentException("");
            }

            BoolGenericResult<Note> result = new BoolGenericResult<Note>();

            Note note = NoteFactory.CreateNote(title, content, type, this.UserAccount, false, noteReceivers, this.UserAccount.Name);
            result.Object = note;

            return result;
        }
    }
}
